﻿using System;
using System.Collections.Generic;
using System.Text;
using Geometry;
using System.Collections.ObjectModel;

namespace PotentialFieldDriver.robotrace.utils.pitch
{
    class Cell
    {
        private Point2D upperLeftPoint;
        private Point2D upperRightPoint;
        private Point2D bottomLeftPoint;
        private Point2D bottomRightPoint;
        private bool isObstacleIn = false;
        private double width;
        private double height;

        public Cell(Point2D upperLeftPoint, double width, double height, ReadOnlyCollection<Wall2D> walls)
        {
            this.upperLeftPoint = upperLeftPoint;
            this.upperRightPoint = new Point2D(upperLeftPoint.x + width, upperLeftPoint.y);
            this.bottomLeftPoint = new Point2D(upperLeftPoint.x, upperLeftPoint.y + height);
            this.bottomRightPoint = new Point2D(upperLeftPoint.x + width, upperLeftPoint.y + height);
            this.isObstacleIn = isAnyWallIntersectingCell(walls);
            this.width = width;
            this.height = height;
        }

        public bool Reachable
        {
            get;
            set;
        }

        public Point2D getUpperLeftPoint()
        {
            return upperLeftPoint;
        }

        public Point2D getUpperRightPoint()
        {
            return upperRightPoint;
        }

        public Point2D getBottomLeftPoint()
        {
            return bottomLeftPoint;
        }

        public Point2D getBottomRightPoint()
        {
            return bottomRightPoint;
        }

        public bool isObstacle()
        {
            return isObstacleIn;
        }

        public void setIsObstacle(bool obstacle)
        {
            this.isObstacleIn = obstacle;
        }

        public double getWidth()
        {
            return width;
        }

        public double getHeight()
        {
            return height;
        }

        private bool isAnyWallIntersectingCell(ReadOnlyCollection<Wall2D> walls)
        {
            foreach (Wall2D wall in walls)
            {
                if (isWallIntersectingCell(wall))
                {
                    return true;
                }
            }
            return false;
        }

        private bool isWallIntersectingCell(Wall2D wall)
        {
            if (CollisionChecker.checkCollicionSection(upperLeftPoint, upperRightPoint, wall.firstPoint, wall.secondPoint) ||
                CollisionChecker.checkCollicionSection(upperLeftPoint, bottomLeftPoint, wall.firstPoint, wall.secondPoint) ||
                CollisionChecker.checkCollicionSection(bottomLeftPoint, bottomRightPoint, wall.firstPoint, wall.secondPoint) ||
                CollisionChecker.checkCollicionSection(bottomRightPoint, upperRightPoint, wall.firstPoint, wall.secondPoint))
            {
                return true;
            }
            //TODO: what if wall is completely INSIDE the cell?
            return false;
        }
    }
}
